﻿
using System;
using System.Collections.Generic;
using System.Data;
using SAS.Core.Entity;
using SAS.Core.Result;

namespace SAS.DataAccess
{
    /// <summary>
    /// 
    /// </summary>
    public class CloneDAO : BaseDAO
    {
        #region Contructors
        protected CloneDAO() : base(string.Empty)
        {

        }
        public CloneDAO(string tokenKey)
            : base(tokenKey)
        {

        }
        #endregion

        /// <summary>
        /// Initialize cloning process.
        /// </summary>
        /// <param name="clientId">Client Id</param>
        /// <param name="pkWorkspaceId">Workspace Id</param>
        /// <param name="sourceEnvironment">Source environment</param>
        /// <param name="destinationEnvironment">Destination environment</param>
        /// <returns></returns>
        public SASResult<string> InitializeCloningProcess(int clientId, string pkWorkspaceId, string sourceEnvironment, string destinationEnvironment)
        {
            //Indentify SQL store procedure name
            const string storeProcedureName = "[scmAdminApp].[uspClone_InitializeByToken]";
            
            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                {  
                    new Parameter {Name = "@Token", Value = TokenKey},
                    new Parameter {Name = "@ClientID", Value = clientId},
                     new Parameter
                        {
                            Name = "@pkClone_ID",
                            DbType = SqlDbType.Int,
                            ParameterDirection = ParameterDirection.Output,
                            OutputReturnName = "CloneID"
                        },
                    new Parameter {Name = "@pkWorkSpace_ID", Value = pkWorkspaceId},
                    new Parameter {Name = "@EnvironmentSource", Value = sourceEnvironment},
                    new Parameter {Name = "@EnvironmentDest", Value = destinationEnvironment}
                };

            //Execute SQL
            return DB.ExecuteScalar<string>(storeProcedureName, sqlParameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cloneID"></param>
        /// <param name="objectName"></param>
        /// <param name="xmlString"></param>
        /// <returns></returns>
        public SASResult<string> AddObject(int cloneID, string objectName, string xmlString)
        {
            //Indentify SQL store procedure name
            const string storeProcedureName = "[scmAdminApp].[uspClone_AddObjectByToken$_ID]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                {
                    new Parameter {Name = "@Token", Value = TokenKey},
                    new Parameter {Name = "@pkClone_ID", Value = cloneID},
                    new Parameter {Name = "@CloneObjectTypeName", Value = objectName},
                    new Parameter {Name = "@XMLString", Value = xmlString}
                };

            //Execute SQL
            return DB.ExecuteScalar<string>(storeProcedureName, sqlParameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cloneObjectID"></param>
        /// <param name="cloneStatusName"></param>
        /// <returns></returns>
        public SASResult<string> ModifyStatusByToken(int cloneObjectID, string cloneStatusName)
        {
            //Indentify SQL store procedure name
            const string storeProcedureName = "[scmAdminApp].[uspCloneObject_ModStatusByToken$_ID]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                {
                    new Parameter {Name = "@Token", Value = TokenKey},
                    new Parameter {Name = "@pkCloneObject_ID", Value = cloneObjectID},
                    new Parameter {Name = "@CloneObjectStatusName", Value = cloneStatusName}
                };

            //Execute SQL
            return DB.ExecuteScalar<string>(storeProcedureName, sqlParameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cloneId"></param>
        /// <param name="objectName"></param>
        /// <param name="xmlString"></param>
        /// <returns></returns>
        public SASResult<string> GetObject(int cloneId, string objectName)
        {
            //Indentify SQL store procedure name
            const string storeProcedureName = "[scmAdminApp].[uspClone_GetObjectByToken$_ID]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                {
                    new Parameter {Name = "@Token", Value = TokenKey},
                    new Parameter {Name = "@pkClone_ID", Value = cloneId},
                    new Parameter {Name = "@CloneObjectTypeName", Value = objectName},
                    new Parameter
                        {
                            Name = "@XMLString",
                            Value = string.Empty,
                            OutputReturnName = "XMLString",
                            ParameterDirection = ParameterDirection.Output,
                            DbType = SqlDbType.Xml
                        }
                };

            //Execute SQL
            return DB.ExecuteScalar<string>(storeProcedureName, sqlParameters);
        }

        public SASResult<IList<CloneObjectInfo>> GetObjectsInfo(int cloneId)
        {
            //Indentify SQL store procedure name
            const string storeProcedureName = "[scmAdminApp].[uspClone_GetObjectInfoByToken$_ID]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                {
                    new Parameter {Name = "@Token", Value = TokenKey},
                    new Parameter {Name = "@pkClone_ID", Value = cloneId}
                };

            //Indentify structure that parse SQL results to objects.
            Func<IDataReader, CloneObjectInfo> make = reader => new CloneObjectInfo
                {
                    CloneObjectId = Convert.ToInt32(reader["pkCloneObject_ID"]),
                    CloneObjectStatusName = Convert.ToString(reader["CloneObjectStatusName"]),
                    CloneObjectTypeName = Convert.ToString(reader["CloneObjectTypeName"]),
                };
            
            //Execute SQL
            return DB.ReadList(storeProcedureName, make, sqlParameters);
        } 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="clientId"></param>
        /// <returns></returns>
        public SASResult<string> Dispose(int clientId)
        {
            //Indentify SQL store procedure name
            const string storeProcedureName = "[scmAdminApp].[uspClone_DisposeByToken]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                {
                    new Parameter {Name = "@Token", Value = TokenKey},
                    new Parameter {Name = "@ClientID", Value = clientId}
                };

            return DB.ExecuteScalar<string>(storeProcedureName, sqlParameters);
        }
    }
}